package com.right.health.app.ui.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.magics.http.utils.AsyncTaskEx;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.AppUserResponse;
import com.right.health.app.bean.ChooseMemberInfo;
import com.right.health.app.constants.AppConfig;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.hyphenate.EMClientHelper;
import com.right.health.app.net.UserApi;
import com.right.health.app.ui.main.MainActivity;

/*
 * Created by hopeliao on 2019-09-24
 *
 */
public class RegisterStep2Activity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private EditText mPwdEt, mPwdConfirmEt;

    private ImageView mSwitchPwdVisibleImage, mSwitchConfirmPwdVisibleImage;

    private boolean mPasswordConfirmVisible = false, mPasswordVisible = false;


    private UserApi mUserApi;

    private AppUserResponse.AppUserInfo mUserInfo;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.register2);
    }

    @Override
    public void findView() {
        mPwdEt = (EditText) findViewById(R.id.et_pwd);
        mSwitchPwdVisibleImage = (ImageView) findViewById(R.id.iv_pwd_change_visible);
        mSwitchPwdVisibleImage.setOnClickListener(this);

        mPwdConfirmEt = (EditText) findViewById(R.id.et_confirm_pwd);
        mSwitchConfirmPwdVisibleImage = (ImageView) findViewById(R.id.iv_pwd_confirm_change_visible);
        mSwitchConfirmPwdVisibleImage.setOnClickListener(this);

        findViewById(R.id.btn_register).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey(BroadCastKey.DATA)) {
            mUserInfo = bundle.getParcelable(BroadCastKey.DATA);

            mUserApi = new UserApi();
            mUserApi.setListener(this);
        }

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_pwd_change_visible) {
            mPasswordVisible = !mPasswordVisible;
            if (mPasswordVisible) {
                mPwdEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                mPwdEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            mPwdEt.setSelection(mPwdEt.getText().length());
            mSwitchPwdVisibleImage.setImageResource(mPasswordVisible ?
                    R.mipmap.ic_pwd_change_visible2 :
                    R.mipmap.ic_pwd_change_visible1);
        } else if (v.getId() == R.id.iv_pwd_confirm_change_visible) {

            mPasswordConfirmVisible = !mPasswordConfirmVisible;
            if (mPasswordConfirmVisible) {
                mPwdConfirmEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                mPwdConfirmEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            mPwdConfirmEt.setSelection(mPwdConfirmEt.getText().length());
            mSwitchConfirmPwdVisibleImage.setImageResource(mPasswordConfirmVisible ?
                    R.mipmap.ic_pwd_change_visible2 :
                    R.mipmap.ic_pwd_change_visible1);
        } else if (v.getId() == R.id.btn_register) {
            if (mUserInfo == null) {
                return;
            }
            String pwd1 = mPwdEt.getText().toString().trim();
            String pwdConfim = mPwdConfirmEt.getText().toString().trim();
            if (TextUtils.isEmpty(pwd1)) {
                ToastUtil.show(App.getInstance(), R.string.please_enter_pwd);
                return;
            }
            if (!pwd1.equals(pwdConfim)) {
                ToastUtil.show(App.getInstance(), R.string.please_enter_confirm_pwd);
                return;
            }

            mUserApi.registerPwd(mUserInfo.mobilephone, pwd1);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(App.getInstance(), response.error);
        } else {

            if (response.targetData instanceof AppUserResponse) {
                AppUserResponse userResponse = response.cast(AppUserResponse.class);

                new AsyncTaskEx<String, Void, Boolean>() {

                    @Override
                    protected Boolean doInBackground(String... strings) {
                        String personId = strings[0];
                        String username = AppConfig.EM_DEFAULT_USER + personId;
                        String pwd = AppConfig.EM_DEFAULT_PASSWORD;
                        boolean isSucc = EMClientHelper.getInstance().createCount(username, pwd);
                        if(isSucc) {
                            EMClientHelper.getInstance().login(username, pwd);
                            App.getInstance().refreshUser(userResponse.user);
                            App.getInstance().refreshChooseMember(new ChooseMemberInfo(userResponse.user.memberid, userResponse.user.sex));

                            return true;
                        }
                        return false;
                    }

                    @Override
                    protected void onPostExecute(Boolean result) {
                        if(result) {
                            sendBroadcast(BroadCastCode.LOGIN_CHANGE);
                            ToastUtil.show(App.getInstance(), "注册成功");

                            startIntent(MainActivity.class);
                            finish();
                        }
                    }
                }.execute(userResponse.user.personid);
            }
        }
    }
}
